package kr.hkit.powerjava.chapter25.chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class QuizServer {
	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket=null;
		PrintWriter out=null;
		BufferedReader in=null;
		try {
			 serverSocket= new ServerSocket(5555);
		} catch (IOException e) {
		System.err.println("다음의 포트 번호에 연결할 수 없습니다.:5555");
		System.exit(-1);
		}
		Socket clientSocket=null;
		try {
			clientSocket=serverSocket.accept();
		} catch (IOException e) {
			System.err.println("accept() fail");
			System.exit(-1);
		}
		out=new PrintWriter(clientSocket.getOutputStream(),true);
		in= new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
		
		String inputLine;
		String outputLine;
		QuizProtocol qp= new QuizProtocol();
		
		outputLine=qp.process(null);
		out.println(outputLine);//클라이언트에게 시작됨을 전송"퀴즈를 시작합니다(y/n)"
		while((inputLine=in.readLine())!=null){//클라이언트에서 받는것을 보낸다
			outputLine=qp.process(inputLine);
			out.println(outputLine);
			if(outputLine.equalsIgnoreCase("quit"));{
				break;
			}
			
		}in.close();
		out.close();clientSocket.close();serverSocket.close();
	}
}
